/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.lib.tools;

import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import static tmp.ac.lib.tools.files.copyFolder;
import tmp.ac.lib.tools.uiUtil;
import tmp.ac.options.urls;
import tmp.ac.ui.setting.menuSetting;

/**
 *
 * @author empcl_000
 */
public class files {

    public String getFileURL(String url) {

        URL FileUrl = getClass().getResource(url);

        return FileUrl.toString();

    }

    public static boolean isExist(String url) {
        boolean r = false;

        File f = new File(url);
        if (f.exists()) {

            r = true;
        }

        return r;

    }

    public void createfolder(String folname, String name, String srcfold) {
        String srcfolder = srcfold;
        String folderName = folname;
        String fname = name;
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter
                = new SimpleDateFormat("MM-dd-yy");
        String dateNow = formatter.format(currentDate.getTime());
        System.out.println(dateNow);
        File srcFolder = new File(srcfolder);
        File destFolder = new File("//" + folderName + "/ABS Shared Network WLAN/Avventuro Users - ReadWrite/1 - BackUp/" + fname + "/" + dateNow);

        //make sure source exists  
        if (!srcFolder.exists()) {

            System.out.println("Directory does not exist.");
            //just exit  
            System.exit(0);

        } else {

            try {
                copyFolder(srcFolder, destFolder);
            } catch (IOException e) {
                e.printStackTrace();
                //error, just exit  
                System.exit(0);
            }
        }

        System.out.println("Done");
    }

    public static void copyFolder(File src, File dest)
            throws IOException {

        if (src.isDirectory()) {

            //if directory not exists, create it  
            if (!dest.exists()) {
                dest.mkdir();
                System.out.println("Directory copied from "
                        + src + "  to " + dest);
            }

            //list all the directory contents  
            String files[] = src.list();

            for (String file : files) {
                //construct the src and dest file structure  
                File srcFile = new File(src, file);
                File destFile = new File(dest, file);
                //recursive copy  
                copyFolder(srcFile, destFile);
            }

        } else {
            //if file, then copy it  
            //Use bytes stream to support all file types  
            InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dest);

            byte[] buffer = new byte[1024];

            int length;
            //copy the file content in bytes   
            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }

            in.close();
            out.close();
            System.out.println("File copied from " + src + " to " + dest);
        }
    }

    public static void copyfile(String srFile, String srtitle, String dtFile) {
        Path f1 = FileSystems.getDefault().getPath(srFile, srtitle);
        Path f2 = FileSystems.getDefault().getPath(dtFile, srtitle);
        try {
            Files.copy(f1, f2, REPLACE_EXISTING);
            uiUtil.sendMessage(null, srFile, dtFile);

        } catch (IOException ex) {

            uiUtil.sendMessage(null, ex.getMessage(), ex.getMessage());
        }
    }

    public static void createPropertiesfile(String path, Properties prop) {

        try {

            prop.store(new FileOutputStream(path), null);

        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public static void createFolder(String path) {

        File theDir = new File(path);

        if (!theDir.exists()) {
          //  System.out.println("creating directory: " + path);
            boolean result = theDir.mkdir();

           // System.out.println("creating directory: " + result);

           /* if (result) {
              //  System.out.println("DIR created");
            }*/
        }
     

    }

    public Image getScaledImage(Image srcImg, int w, int h) {
        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(srcImg, 0, 0, w, h, null);
        g2.dispose();
        return resizedImg;
    }

    public static String getFolderPath(String Title) {

        String backUpPath = "";
        JFileChooser fc = null;
        if (fc == null) {
            fc = new JFileChooser();
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            fc.setAcceptAllFileFilterUsed(false);
        }
        int returnVal = fc.showDialog(null, Title);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            backUpPath = file.getAbsolutePath();
        }
        return backUpPath;
    }

    public void saveImage(String chDirName, String chFileName) {
        try {

            if (!chDirName.trim().equals("") && !chFileName.trim().equals("")) {
                URI url = this.getClass().getResource(urls.Image_URL).toURI();
                String myurl = url.toString().replace("file:/", "");
                tmp.ac.lib.tools.files.copyfile(chDirName, chFileName, myurl);
            }

        } catch (URISyntaxException ex) {
            uiUtil.sendMessage(null, ex.getMessage(), ex.getMessage());
            Logger.getLogger(menuSetting.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public String FileName = "";
    public String DirName = "";
    public URL url;

    public void saveImage() {
        try {

            if (!DirName.trim().equals("") && !FileName.trim().equals("")) {
                URI url = this.getClass().getResource(urls.Image_URL).toURI();
                String myurl = url.toString().replace("file:/", "");
                copyfile(DirName, FileName, myurl);
            }

        } catch (URISyntaxException ex) {
            Logger.getLogger(menuSetting.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void saveBImage() {
        try {

            if (!DirName.trim().equals("") && !FileName.trim().equals("")) {
                URI url = this.getClass().getResource(urls.Image_URL).toURI();
                String myurl = url.toString().replace("file:/", "");
                copyfile(DirName, FileName, myurl);
            }

        } catch (URISyntaxException ex) {
            Logger.getLogger(menuSetting.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void getImage(Component parent) {
        JFileChooser chooser = new JFileChooser(".");
        int a = chooser.showOpenDialog(parent);
        if (a == JFileChooser.APPROVE_OPTION) {
            try {

                url = chooser.getSelectedFile().toURL();
                FileName = chooser.getSelectedFile().getName();
                DirName = "" + chooser.getCurrentDirectory();

                //   String   updateImage(chooser.getSelectedFile().toURL());
                // chFileName = chooser.getSelectedFile().getName();
                // chDirName = "" + chooser.getCurrentDirectory();
            } catch (Exception ex) {
                uiUtil.sendAlert(parent, "error", ex.getMessage());
            }

        }

    }

}
